#include "questionnaire.h"
#include "system.h"
#include "ui_questionnaire.h"
#include <QDebug>
#include <QSqlQuery>

Questionnaire::Questionnaire(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Questionnaire)
{
    ui->setupUi(this);

    qDebug() << QSqlDatabase::drivers();
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("localhost");//主机名
    m_db.setPort(3306);
    m_db.setDatabaseName("user");
    m_db.setUserName("root");
    m_db.setPassword("123456");
    m_db.open();
    if(m_db.open())
    {
        qDebug() << "连接成功";
        showStuInfo();
    }
    else
    {
        qDebug() << "连接失败！！";
    }
}

Questionnaire::~Questionnaire()
{
    delete ui;
}

void Questionnaire::showStuInfo()
{
    QSqlQuery query;
    query.exec("SELECT * FROM questionnaire");
    while(query.next())
    {
        QString name = query.value(0).toString();
        int age = query.value(1).toInt();
        QString sex = query.value(2).toString();
        QString hoppy = query.value(2).toString();
        qDebug() << QString("name %1 age %2 sex %3 hoppy %4 ").arg(name).arg(age).arg(sex).arg(hoppy);

        QTableWidgetItem* nameItem = new QTableWidgetItem(name);
        QTableWidgetItem* ageItem = new QTableWidgetItem(age);
        QTableWidgetItem* sexItem = new QTableWidgetItem(sex);
        QTableWidgetItem* hoppyItem = new QTableWidgetItem(hoppy);

        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, 0, nameItem);
        ui->tableWidget->setItem(row, 1, ageItem);
        ui->tableWidget->setItem(row, 2, sexItem);
        ui->tableWidget->setItem(row, 3, hoppyItem);
    }
}
//查找按钮
void Questionnaire::on_pushButtonSeek_clicked()
{

    QString info = ui->lineEditSeekName->text();
    QSqlQuery query;
    query.prepare("SELECT * FROM questionnaire WHERE name LIKE ? ");
    query.addBindValue("%" + info + "%");
    if(query.exec())
    {
        qDebug() << "更新成功！";
        ui->tableWidget_2->clearContents();
        ui->tableWidget_2->setRowCount(0);
        while(query.next())
        {
            QString name = query.value(0).toString();
            int age = query.value(1).toInt();
            QString sex = query.value(2).toString();
            QString hoppy = query.value(2).toString();

            qDebug() << QString("name %1 age %2 sex %3 hoppy %4 ").arg(name).arg(age).arg(sex).arg(hoppy);

            QTableWidgetItem* nameItem = new QTableWidgetItem(name);
            QTableWidgetItem* ageItem = new QTableWidgetItem(age);
            QTableWidgetItem* sexItem = new QTableWidgetItem(sex);
            QTableWidgetItem* hoppyItem = new QTableWidgetItem(hoppy);

            int row = ui->tableWidget_2->rowCount();
            ui->tableWidget_2->insertRow(row);
            ui->tableWidget->setItem(row, 0, nameItem);
            ui->tableWidget->setItem(row, 1, ageItem);
            ui->tableWidget->setItem(row, 2, sexItem);
            ui->tableWidget->setItem(row, 3, hoppyItem);

        }
    }
    else
    {
        qDebug() << "更新失败！";
    }

}




void Questionnaire::on_pushButton_clicked()
{
    System* m = new System();
    m->show();
    this->close();
}
